// pages/recite/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentPage: 0,
    correctCount: 0,
    score: '',
    subjects: [],
    checkid: -1,
    checkTrue: false,
    tipText: '',
    mode: 1
  },

  startRecite() {
    this.setData({ currentPage: 1 })
  },
  
  toNext() {
    if (this.data.checkid < 0) {
      wx.showToast({
        title: 'Please Answer First',
        icon: 'none'
      })
      return
    }
    if (this.data.currentPage > 9) {
      wx.showToast({
        title: 'No More',
        icon: 'none'
      })
      return
    }
    this.setData({ checkid: -1, tipText: '' })
    let { currentPage } = this.data
    this.setData({ currentPage: ++currentPage })
  },

  toResult() {
    if (this.data.checkid < 0) {
      wx.showToast({
        title: 'Please Answer First',
        icon: 'none'
      })
      return
    }
    let { currentPage, correctCount } = this.data
    this.setData({ currentPage: ++currentPage, score: `${correctCount * 10}%`})
  },

  choiceHandler(e) {
    if (this.data.checkid > -1) {
      wx.showToast({
        title: 'Press [Next], Please',
        icon: 'none'
      })
      return
    }
    const isTrue = e.currentTarget.dataset.istrue
    const checkId = e.currentTarget.dataset.checkid
    let { correctCount } = this.data
    correctCount += isTrue ? 1 : 0
    this.setData({ checkid: checkId, tipText: isTrue ? 'Correct!' : 'You got wrong', checkTrue: isTrue, correctCount })
  },

  goHome() {
    wx.switchTab({
      url: '/pages/home/index',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ mode: wx.getStorageSync('vision-mode') })
    wx.showLoading({
      title: 'Get Subjects...',
      mask: true
    })
    wx.cloud.callFunction({
      name: 'getSubjectList'
    }).then(result => {
      wx.hideLoading()
      if (result.result.length < 10) {
        wx.showToast({
          title: 'The number of your words is less than <10>, please enter some more.',
          icon: 'error',
          duration: 1500,
          mask: true
        })
        wx.switchTab({
          url: '/pages/home/index',
        })
      } else {
        this.setData({ subjects: result.result })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})